package com.cipta.lomob.adapter;

import java.util.LinkedList;

import com.cipta.lomob.R;
import com.cipta.lomob.components.CropCircleTransformation;
import com.cipta.lomob.model.Users;
import com.squareup.picasso.Picasso;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class AdapterUsers extends BaseAdapter{
	Activity activity;
	LayoutInflater inflater;
	LinkedList<Users> listUser;
	
	public AdapterUsers(Activity activity, LinkedList<Users> listUsers) {
		// TODO Auto-generated constructor stub
		this.activity = activity;
		inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.listUser = listUsers;
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return listUser.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	@SuppressLint("NewApi")
	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		// TODO Auto-generated method stub
		View view = arg1;
		ViewHolder holder = null;
		
		if (view == null) {
			holder = new ViewHolder();
			view = inflater.inflate(R.layout.inf_invite_friends, null);
			holder.txtUsername = (TextView)view.findViewById(R.id.inf_txt_invite_friends_name);
			holder.imgUser = (ImageView)view.findViewById(R.id.inf_invite_friends_avatar);
			holder.btnAction = (Button)view.findViewById(R.id.btn_connected_friends_action);
			view.setTag(holder);
		} else {
			holder = (ViewHolder)view.getTag();
		}
		
		
		holder.txtUsername.setText(listUser.get(arg0).getName());
		if(listUser.get(arg0).getAvatar().equalsIgnoreCase("")){
			holder.imgUser.setImageResource(R.drawable.user1);
		}else{
			Picasso.with(activity).load(listUser.get(arg0).getAvatar()).placeholder(R.drawable.user1).transform(new CropCircleTransformation()).into(holder.imgUser);
		}
		return view;
	}
	
	static class ViewHolder{
		TextView txtUsername;
		ImageView imgUser;
		Button btnAction;
	}

}
